package com.jiaai.web.zfp_back.privilege;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jiaai.exception.ServerException;
import com.jiaai.privilege.api.IGoodNoApi;
import com.jiaai.privilege.model.GoodNoVO;
import com.jiaai.util.RestResultGenerator;
import org.springframework.web.bind.annotation.*;

/**
 * class_name:ZBGoodNoController
 * describe:TODO
 * create_user:张昉平
 * create_date:2018/1/4
 * create_date:16:32
 */
@RestController
@RequestMapping("/api/v1/admin/gn")
public class ZBGoodNoController {

    @Reference
    private IGoodNoApi iGoodNoApi;

    @RequestMapping("/list")
    public Object list(@RequestParam Integer curr, String gnNo)throws ServerException {
        return  RestResultGenerator.genSuccessResult(iGoodNoApi.getAll(curr, gnNo));
    }

    @PostMapping("/saveoredit")
    public Object saveOrEdit(@RequestBody GoodNoVO goodNoVO)throws ServerException {
        try {
            iGoodNoApi.saveOrEdit(goodNoVO);
            return  RestResultGenerator.genSuccessResult("操作成功");
        }catch (Exception e){
            if (e.getMessage().equals("EXIST")){
                return RestResultGenerator.genErrorResult(600, "靓号已经存在啦!");
            }
            throw new RuntimeException("服务器挂了!");
        }
    }

    @RequestMapping("/delete/{id}/{flag}")
    public Object delete(@PathVariable("id") Integer id, @PathVariable("flag") Integer flag)throws ServerException {
        iGoodNoApi.delete(id, flag);
        return  RestResultGenerator.genSuccessResult("操作成功");
    }

    @RequestMapping("/deleteById/{id}")
    public Object deleteById(@PathVariable("id") Integer id)throws ServerException {
        try {
            iGoodNoApi.deleteById(id);
            return  RestResultGenerator.genSuccessResult("操作成功");
        } catch (Exception e) {
            throw new RuntimeException("删除失败!");
        }
    }

}
